library(cjbart)
library(cjoint)
library(dplyr)


conjoint_data <- read.qualtrics("Israel_2024_survey.csv",
                                responses = NULL,
                                covariates = c("Q4", "Q6", "Q7",	"Q8", "Q9",
                                               "Q10_1",	"Q10_2",	"Q10_4",	"Q10_18",	"Q10_7",	"Q10_8",	"Q10_9",	"Q10_10",	"Q10_11",	"Q10_12",	"Q10_13",	"Q10_14", "Q10_15", "Q10_17",
                                               "Q11_1",	"Q11_2", "Q11_3"	,"Q11_4"	,"Q11_5"	,"Q11_7",	"Q11_8"	,"Q11_9",	
                                               "Q10.1",	"Q10.2", "Q10.3", "Q10.4"	,"Q10.5", "Q10.6",	"Q10.7",
                                               "Q14_15",
                                               "Q13_1",	"Q13_2",	"Q13_3",	"Q13_4",	"Q13_5",	"Q13_6",	"Q13_7"	,"Q13_8",
                                               "Q17", "Q20.1_1",	"Q20.1_2",	"Q20.1_3",	"Q20.1_4"	,"Q20.1_5"),
                                respondentID = "cintID",
                                ranks = c("Q23_1", "Q24_1", "Q25_1" , "Q26_1", "Q27_1", "Q23_2", "Q24_2", "Q25_2" , "Q26_2", "Q27_2"))


## Translate Hebrew into English
Israeldemoc <- conjoint_data %>% 
  select(!c(contains("rowpos")))

Israeldemoc <- Israeldemoc %>%
  mutate(
    Democraticrules= case_when(
      Adherence.to.the.democratic.rules == "בעד"~"Support.d",
      Adherence.to.the.democratic.rules== "נגד"~"Oppose.d"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    Authoritarian = case_when(
      authoritarian.leadership ==  "בעד"~"Support.a",
      authoritarian.leadership ==  "נגד"~"Oppose.a"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    Commitment = case_when(
      commitment ==  "בעד"~"Support.c",
      commitment ==  "נגד"~"Oppose.c"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    Equalrights = case_when(
      equal.rights ==  "בעד"~"Support.e",
      equal.rights ==  "נגד"~"Oppose.e"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    FreedomOfSpeech = case_when(
      freedom.of.speech == "בעד"~"Support.f",
      freedom.of.speech ==  "נגד"~"Oppose.f"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    JudicialIndependent = case_when(
      judicial.independent ==  "בעד"~"Support.j",
      judicial.independent ==  "נגד"~"Oppose.j"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    tGovernment = case_when(
      trust.in.gov ==  "אמון  מועט"~"Trust very much.g",
      trust.in.gov == "אמון רב"~"Trust.g",
      trust.in.gov == "אמון רב  מאד"~"Not trust.g",
      trust.in.gov == "כלל אין  אמון"~"Never trust.g"
    )
  )
Israeldemoc <- Israeldemoc %>%
  mutate(
    tKnesset = case_when(
      trust.in.knesset ==  "אמון  מועט"~"Trust very much.k",
      trust.in.knesset == "אמון רב"~"Trust.k",
      trust.in.knesset == "אמון רב  מאד"~"Not trust.k",
      trust.in.knesset == "כלל אין  אמון"~"Never trust.k"
    )
  )

Israeldemoc <- Israeldemoc %>%
  mutate(
    tSupremeCourt = case_when(
      trust.in.supreme.court == "אמון  מועט"~"Trust very much.s",
      trust.in.supreme.court == "אמון רב"~"Trust.s",
      trust.in.supreme.court == "אמון רב  מאד"~"Not trust.s",
      trust.in.supreme.court == "כלל אין  אמון"~"Never trust.s"
    )
  )



## Combine Attribute Levels
Israeldemoc <- Israeldemoc |>
  mutate(tGovernment = case_when(
    tGovernment %in% c("Not trust.g", "Never trust.g") ~ "Not trust.g",
    tGovernment %in% c("Trust.g", "Trust very much.g") ~ "Trust.g",
    TRUE ~ tGovernment 
  ))

Israeldemoc <- Israeldemoc |>
  mutate(tKnesset = case_when(
    tKnesset %in% c("Not trust.k", "Never trust.k") ~ "Not trust.k",
    tKnesset %in% c("Trust.k", "Trust very much.k") ~ "Trust.k",
    TRUE ~ tKnesset
  ))

Israeldemoc <- Israeldemoc |>
  mutate(tSupremeCourt = case_when(
    tSupremeCourt %in% c("Not trust.s", "Never trust.s") ~ "Not trust.s",
    tSupremeCourt %in% c("Trust.s", "Trust very much.s") ~ "Trust.s",
    TRUE ~ tSupremeCourt
  ))



## Convert into a Facor
Israeldemoc$Democraticrules <- as.factor(Israeldemoc$Democraticrules)
Israeldemoc$Authoritarian <- as.factor(Israeldemoc$Authoritarian)
Israeldemoc$Commitment <- as.factor(Israeldemoc$Commitment)
Israeldemoc$Equalrights <- as.factor(Israeldemoc$Equalrights)
Israeldemoc$FreedomOfSpeech <- as.factor(Israeldemoc$FreedomOfSpeech)
Israeldemoc$JudicialIndependent <- as.factor(Israeldemoc$JudicialIndependent)
Israeldemoc$tGovernment <- as.factor(Israeldemoc$tGovernment)
Israeldemoc$tKnesset <- as.factor(Israeldemoc$tKnesset)
Israeldemoc$tSupremeCourt <- as.factor(Israeldemoc$tSupremeCourt)


Israeldemoc <- na.omit(Israeldemoc)



## cjmodel/BART
set.seed(100)

cj_model <- cjbart(
  data = Israeldemoc,
  Y = "selected",
  id = "respondentIndex",
  round = "task",
  use_round = TRUE,
  cores = 1 )



## IMCE
het_effects <- IMCE(data = Israeldemoc,
                    model = cj_model,
                    attribs = c("Democraticrules", "Authoritarian", "Commitment", "Equalrights", "FreedomOfSpeech", "JudicialIndependent", "tGovernment", "tKnesset", "tSupremeCourt"),
                    ref_levels = c("Oppose.d", "Oppose.a", "Oppose.c", "Oppose.e", "Oppose.f", "Oppose.j", "Not trust.g", "Not trust.k", "Not trust.s"),
                    cores = 1)


summary(het_effects)


##plot(het_effects, covar = "Q11_1", se = TRUE) +
  #theme_minimal() +
  #scale_color_gradient(low = "#c6dbef", high = "#08306b")





## VIMP

vimp_estimates <- het_vimp(imces = het_effects, cores = 1)

summary(vimp_estimates)

#plot(vimp_estimates, covars = c("Q11_1", "Q11_2", "Q13_6", "Q14_15", "Q10.7", "Q10_1"))

